<template>
  <div class="employees-container">
    <div class="app-container">
      <PageTools>
        <template #left>
          <i class="el-icon-info" />
          <span>共{{ record }}条记录</span>
        </template>
        <template #right>
          <el-button type="warning" size="small"><router-link to="/ImportExcel">excel导入</router-link></el-button>
          <el-button type="danger" size="small" @click="exportEmployee">excel导出</el-button>
          <el-button type="primary" size="small" @click="addEmploy">新增员工</el-button>
        </template>
      </PageTools>
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column width="60" label="序号" type="index" :index="fn" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像">
            <template #default="{row}">
              <el-image :src="row.staffPhoto" class="avata">
                <template #error>
                  <img src="@/assets/common/bigUserHeader.png" alt="">
                </template>
              </el-image>

            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template #default="{row}">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>

          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间">
            <template #default="{row}">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="200">
            <template #default="{row}">
              <el-button type="text" size="small" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="deletelist(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 表格 -->
    <add-employee :visi-ble="visiBle" @close-dialog="closeDialog" @fetc-hList="fetchList" />

    <!-- 分页组件 -->
    <div style="height: 60px; margin-top: 10px">
      <el-pagination
        :total="total"
        :current-page="params.page"
        :page-size="params.size"
        layout="prev, pager, next"
        @current-change="pageChange"
      />
    </div>
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employees'
import dayjs from 'dayjs'
import addEmployee from '@/views/Employee/components/add-employee.vue'
import { getExportData } from '@/utils/excel'
export default {
  name: 'Employee',
  components: {
    addEmployee
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        size: 10
      },
      total: 0,
      visiBle: false,
      record: ''
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    // 获取列表数据
    async fetchList() {
      const { rows, total } = await getEmployeeList(this.params)
      this.list = rows
      this.total = total
      console.log(this.list)
    },
    formatEmployment(a) {
      const res = {
        1: '正式',
        2: '非正式'
      }
      return res[a]
    },
    // 格式化日期
    formatDate(a) {
      return dayjs(a).format('YYYY-MM-DD')
    },

    // 弹窗显示
    addEmploy() {
      this.visiBle = true
    },
    // 弹窗关闭
    closeDialog() {
      this.visiBle = false
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.fetchList()
    },
    fn(index) {
      this.record = index + this.params.page * this.params.size - this.params.size + 1
      return index + this.params.page * this.params.size - this.params.size + 1
    },

    // 删除功能
    deletelist(id) {
      this.$confirm('确定要删除吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delEmployee(id)
        this.fetchList()
        this.$message.success('删除成功')
      })
    },
    // 导出功能
    exportEmployee() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await getEmployeeList(this.params)
        const headerRelation = {
          '姓名': 'username',
          '手机号': 'mobile',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '聘用形式': 'formOfEmployment',
          '部门': 'departmentName'
        }
        const reso = {
          1: '正式',
          2: '非正式'
        }
        res.rows.forEach(item => {
          item.formOfEmployment = reso[item.formOfEmployment]
        })

        const { data } = getExportData(res.rows, headerRelation)

        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header: Object.keys(headerRelation), // 表头header 必填 要求是一个数组
          data, // 表格数据body 要求是一个二维数组
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },

    // 员工详情
    goDetail(id) {
      this.$router.push({
        path: '/Detail',
        query: {
          id
        }
      })
    },
    fb(img) {
      console.log(img)
    }
  }
}
</script>

<style lang="scss" scoped>
.avata img{
  width: 50%;
}
</style>
